/*
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
#include "../../Input.hpp"
#include <SDL/SDL_version.h>

Uint8 *Keys; // Keys2 is bigger than we need, works though
int NumKeys;
Uint8 Keys2[512]; // Second one to watch state changes

void CheckEvents() // Allows us to quit correctly
{
	SDL_Event Event;
	
	while (SDL_PollEvent(&Event))
	{
		if(Event.type == SDL_QUIT)
		{
			ShutDownProgram();
		}
		//TODO: Make the above cleaner
		/*if(Event.type == SDL_MOUSEBUTTONDOWN ) // We Don't care about the Mouse Wheel
		 {
		 if(Event.button.button == SDL_BUTTON_WHEELUP)
		 printf("Wheel Up\n");
		 else if(Event.button.button == SDL_BUTTON_WHEELDOWN)
		 printf("Wheel Down\n");
		 }*/
	}
}
void GetInput() // Updates the keys array to new info
{
#if (SDL_MINOR_VERSION == 3)
	Keys = SDL_GetKeyboardState(&NumKeys);
#else
	Keys = SDL_GetKeyState(&NumKeys);
#endif
}
bool MousePressed(int Button)
{
#if (SDL_MINOR_VERSION == 3)
	if(Button & SDL_GetMouseState(SDL_SelectMouse(-1), NULL, NULL))
#else
		if(Button & SDL_GetMouseState(NULL, NULL))
#endif
			return true;
		else
			return false;
}
void MousePosition(int *X, int *Y)
{
#if SDL_MINOR_VERSION == 3
	SDL_GetMouseState(SDL_SelectMouse(-1), X, Y);
#else
	SDL_GetMouseState(X, Y);
#endif
}
bool ModPressed(int Key)
{
	if(SDL_GetModState() & Key)
		return true;
	else
		return false;
}
bool KeyPressed(int Key) // Is Key Pressed?
{
#if SDL_MINOR_VERSION == 3
	int _Key = SDL_GetScancodeFromKey(Key);
#else
	int _Key = Key;
#endif
	if(Keys != NULL && _Key <= NumKeys && Keys[_Key] == true && Keys2[_Key] == false)
		Keys2[_Key] = true;
	else
		Keys2[_Key] = false;
	return Keys2[_Key];
}
bool KeyClicked(int Key) // Was it clicked? Doesn't Work for some reason
{
#if SDL_MINOR_VERSION == 3
	int _Key = SDL_GetScancodeFromKey(Key);
#else
	int _Key = Key;
#endif
	if(Keys != NULL && _Key <= NumKeys && Keys[_Key] == false && Keys2[_Key] == true)
		Keys2[_Key] = false;
	return Keys2[_Key];
}
bool PeekKeyClicked(int Key) // Advanced KeyClicked
{
	if(Keys != NULL && Keys[Key] == false && Keys2[Key] == true)
		return true;
	else
		return false;
}

